package com.dofast.bt.util;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseApplication;

/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：帮助工具类
 */

public class Helper {
    private static Toast toast;

    /**
     * 吐司
     *
     * @param msg 吐司显示信息
     */
    public static void showToast(String msg) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(BaseApplication.getInstance().context, msg, Toast.LENGTH_LONG);
        toast.show();
    }

    public static boolean isStrEmpty(String str) {
        if (str == null || str.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * @param fragmentActivity
     * @param f
     * @param animIndex        等于0-无进出动画， 大于0-左出右进， 小于0-右出左进
     */
    public static void changeFragment(FragmentActivity fragmentActivity, int replaceLayoutId, Fragment f, int animIndex) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        if (animIndex > 0) {
            ft.setCustomAnimations(R.anim.slide_right_in,
                    R.anim.slide_left_out, R.anim.slide_left_in,
                    R.anim.slide_right_out);
        } else if (animIndex < 0) {
            ft.setCustomAnimations(R.anim.slide_left_in,
                    R.anim.slide_right_out, R.anim.slide_right_in,
                    R.anim.slide_left_out);
        } else {

        }
        ft.replace(replaceLayoutId, f);
//        ft.replace(replaceLayoutId, f, f.getClass().getSimpleName());
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }
    /**
     * 自适应宽度加载图片。保持图片的长宽比例不变，通过修改imageView的高度来完全显示图片。
     */
    public static void loadIntoUseFitWidth(Context context, final String imageUrl, int errorImageId, final ImageView imageView) {
        Glide.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .placeholder(errorImageId)
                .error(errorImageId)
                .into(imageView);
    }
}
